package com.tencent.qcloud.tim.uikit.modules.group.member;


import android.graphics.Color;
import android.text.TextUtils;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.noober.background.drawable.DrawableCreator;
import com.tencent.imsdk.v2.V2TIMGroupMemberFullInfo;
import com.tencent.qcloud.tim.uikit.R;
import com.tencent.qcloud.tim.uikit.component.picture.imageEngine.impl.GlideEngine;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

import com.manor.common.utils.DpUtil;
import com.manor.common.utils.StringUtil;

public class GroupMemberManagerAdapter extends BaseQuickAdapter<GroupMemberInfo, BaseViewHolder> {
    private IGroupMemberChangedCallback mCallback;

    public void setMemberChangedCallback(IGroupMemberChangedCallback callback) {
        this.mCallback = callback;
    }

    public GroupMemberManagerAdapter(int layoutResId, @Nullable List<GroupMemberInfo> data) {
        super(layoutResId, data);
        //addChildClickViewIds(R.id.);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, GroupMemberInfo item) {

        holder.setText(R.id.group_member_name, StringUtil.isNotEmpty(item.getNameCard()) ?
        item.getNameCard() : item.getNickName());//item.getAccount()
        if (!TextUtils.isEmpty(item.getIconUrl()))
            GlideEngine.loadCircleImage((ImageView) holder.getView(R.id.group_member_icon),
                    StringUtil.isNotEmpty(item.getIconUrl()) ? item.getIconUrl() : R.mipmap.default_user_icon);

        switch (item.getMemberType()) {
            case V2TIMGroupMemberFullInfo.V2TIM_GROUP_MEMBER_ROLE_OWNER:
                holder.setVisible(R.id.tvTag, true);
                holder.setText(R.id.tvTag, "群主");
                holder.getView(R.id.tvTag).setBackground(new DrawableCreator.Builder()
                        .setCornersRadius(DpUtil.dp2px(8))
                        .setSolidColor(Color.parseColor("#FFD6AC19"))
                        .build());
                break;
            case V2TIMGroupMemberFullInfo.V2TIM_GROUP_MEMBER_ROLE_ADMIN:
                holder.setVisible(R.id.tvTag, true);
                holder.setText(R.id.tvTag, "管理员");
                holder.getView(R.id.tvTag).setBackground(new DrawableCreator.Builder()
                        .setCornersRadius(DpUtil.dp2px(8))
                        .setSolidColor(Color.parseColor("#FF25B22E"))
                        .build());
                break;
            default:
                holder.setVisible(R.id.tvTag, false);
        }

    }
}


//public class GroupMemberManagerAdapter extends BaseAdapter {
//
//    private IGroupMemberChangedCallback mCallback;
//    private GroupInfo mGroupInfo;
//    private List<GroupMemberInfo> mGroupMembers = new ArrayList<>();
//
//    public void setMemberChangedCallback(IGroupMemberChangedCallback callback) {
//        this.mCallback = callback;
//    }
//
//    @Override
//    public int getCount() {
//        return mGroupMembers.size();
//    }
//
//    @Override
//    public GroupMemberInfo getItem(int i) {
//        return mGroupMembers.get(i);
//    }
//
//    @Override
//    public long getItemId(int i) {
//        return 0;
//    }
//
//    @Override
//    public View getView(final int i, View view, final ViewGroup viewGroup) {
//        MyViewHolder holder;
//        if (view == null) {
//            view = LayoutInflater.from(TUIKit.getAppContext()).inflate(R.layout.group_member_adpater, viewGroup,
//            false);
//            holder = new MyViewHolder();
//            holder.memberIcon = view.findViewById(R.id.group_member_icon);
//            holder.memberName = view.findViewById(R.id.group_member_name);
//            view.setTag(holder);
//        } else {
//            holder = (MyViewHolder) view.getTag();
//        }
//        final GroupMemberInfo info = getItem(i);
//        if (!TextUtils.isEmpty(info.getIconUrl()))
//            GlideEngine.loadImage(holder.memberIcon, info.getIconUrl(), null);
//        holder.memberName.setText(info.getAccount());
//        view.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                if (!mGroupInfo.isOwner())
//                    return false;
//                TextView delete = new TextView(viewGroup.getContext());
//                delete.setText(R.string.group_remove_member);
//                int padding = ScreenUtil.getPxByDp(10);
//                delete.setPadding(padding, padding, padding, padding);
//                delete.setBackgroundResource(R.drawable.text_border);
//                int location[] = new int[2];
//                v.getLocationInWindow(location);
//                final PopupWindow window = PopWindowUtil.popupWindow(delete, viewGroup, location[0], location[1] +
//                v.getMeasuredHeight() / 3);
//                delete.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        List<GroupMemberInfo> dels = new ArrayList<>();
//                        dels.add(info);
//                        GroupInfoProvider provider = new GroupInfoProvider();
//                        provider.loadGroupInfo(mGroupInfo);
//                        provider.removeGroupMembers(dels, new IUIKitCallBack() {
//                            @Override
//                            public void onSuccess(Object data) {
//                                mGroupMembers.remove(info);
//                                notifyDataSetChanged();
//                                if (mCallback != null) {
//                                    mCallback.onMemberRemoved(info);
//                                }
//                            }
//
//                            @Override
//                            public void onError(String module, int errCode, String errMsg) {
//                                ToastUtil.toastLongMessage("移除成员失败:errCode=" + errCode);
//                            }
//                        });
//                        window.dismiss();
//                    }
//                });
//                return false;
//            }
//        });
//        return view;
//    }
//
//    public void setDataSource(GroupInfo groupInfo) {
//        if (groupInfo != null) {
//            mGroupInfo = groupInfo;
//            this.mGroupMembers = groupInfo.getMemberDetails();
//            BackgroundTasks.getInstance().runOnUiThread(new Runnable() {
//                @Override
//                public void run() {
//                    notifyDataSetChanged();
//                }
//            });
//        }
//    }
//
//    private class MyViewHolder {
//        private ImageView memberIcon;
//        private TextView memberName;
//    }
//}
